package com.lagou.service.impl;

import com.lagou.dao.ResourceCategoryMapper;
import com.lagou.domain.ResourceCategory;
import com.lagou.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ResourceCategoryServiceImpl implements ResourceCategoryService {

    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;

    /**
     * 查询资源分类列表
     * @return
     */
    @Override
    public List<ResourceCategory> findAllResourceCategory() {

        List<ResourceCategory> allResourceCategory = resourceCategoryMapper.findAllResourceCategory();

        return allResourceCategory;
    }

    /**
     * 新增和修改资源分类
     * @param resourceCategory
     */
    @Override
    public void saveOrUpdateResourceCategory(ResourceCategory resourceCategory) {
        Date date = new Date();
        if(Optional.ofNullable(resourceCategory.getId()).isPresent()){
            resourceCategory.setUpdatedBy("system");
            resourceCategory.setUpdatedTime(date);
            resourceCategoryMapper.updayeResourceCategory(resourceCategory);
        }else{
            resourceCategory.setCreatedBy("system");
            resourceCategory.setCreatedTime(date);
            resourceCategory.setUpdatedBy("system");
            resourceCategory.setUpdatedTime(date);
            resourceCategoryMapper.saveResourceCategory(resourceCategory);
        }
    }

    /**
     * 删除资源分类
     * @param id
     */
    @Override
    public void deleteResourceCategory(int id) {
        resourceCategoryMapper.deleteResourceCategory(id);
    }
}
